﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SES.CMS.BL;
using SES.CMS.DO;

namespace SES.CMS.AdminCP.PageUC
{
    public partial class ucListAlbumRightSlide : System.Web.UI.UserControl
    {
        protected SlideDO objSlide = new SlideDO();
        protected void Page_Load(object sender, EventArgs e)
        {
            gvAtDataSource();

        }

        protected void gvAtDataSource()
        {
            gvAt.DataSource = new SlideBL().SelectByCate(-1);
            gvAt.DataBind();
        }
        protected void gvAt_SelectedIndexChanged(object sender, EventArgs e)
        {
            int SlideID = int.Parse(gvAt.DataKeys[gvAt.SelectedIndex].Value.ToString());
            Response.Redirect("Default.aspx?Page=RightSlide&SlideID=" + SlideID.ToString());
        }
        protected void gvAt_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            new SlideBL().Delete(new SlideDO { SlideID = Convert.ToInt32(gvAt.DataKeys[e.RowIndex].Value) });
            Functions.Alert("Xóa bản tin thành công!", Request.Url.ToString());
        }

        protected void gvAt_RowCommand(object sender, GridViewCommandEventArgs e)
        {
           
        }
        private string UploadFile(FileUpload fulImage)
        {
            if (fuImage.HasFile)
            {
                string FileName = string.Format("{0}{1}", Functions.Change_AV(txtTitle.Text) + "-" + DateTime.Now.ToString("ddMMyyyyhhmmss"), fulImage.FileName.Substring(fulImage.FileName.LastIndexOf(".")));
                string SaveLocation = string.Format("{0}\\{1}", Server.MapPath("~/Media/"), FileName);
                fulImage.SaveAs(SaveLocation);
                return FileName;
            }
            return string.Empty;
        }

        protected void Init()
        {
            objSlide.CategoryID = -1;
            objSlide.Description = txtDescription.Text;
            objSlide.eDescription = txteDescription.Text;
            objSlide.OrderID = 1;
            objSlide.SlideImg = UploadFile(fuImage);
            objSlide.SlideURL = txtLinkAnh.Text;
            objSlide.Title = txtTitle.Text;
            objSlide.eTitle = txteTitle.Text;
 
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            panel.Visible = true;
            btnAdd.Visible = false;
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            Init();
            new SlideBL().Insert(objSlide);
            Functions.Alert("Thêm mới thành công", "Default.aspx?Page=ListAlbumRightSlide");
            panel.Visible = false;
            Response.Redirect("Default.aspx?Page=ListAlbumRightSlide");
            btnAdd.Visible = true;
        }
        protected void btnReset_Click(object sender, EventArgs e)
        {
            panel.Visible = false;
            Response.Redirect("Default.aspx?Page=ListAlbumRightSlide");
            btnAdd.Visible = true;
        }

    }
}